import { ref } from 'vue'
import { adapter, request } from '@/request'
import { updateFreeWordByPost } from '@/core/api/promotion'
import type { Order } from '@/core/interfaces/order'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { VersionTypeEnum, WordTypeEnum } from '@/core/enums/type'
import type { PlatformEnum } from '@/core/enums/platform'
import type { GiftKindEnum } from '@/views/promotion/interface'

export interface FreeWord {
  name: string
  orderId: Order['id']
  orderNo: Order['orderNo']
  clientType?: PlatformEnum
  type: WordTypeEnum
  updateUserId: number
  versionType: VersionTypeEnum
  giftType: GiftKindEnum
}

export default function updateFreeWord() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)

  const fetch = (data: FreeWord[]) => {
    const { url, method } = updateFreeWordByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [undefined, fetch, loading] as const
}
